<?php

class backend_model extends jqxGrid_CI_Model {

    //protected $mytable,$_id;
    function __construct($congifs=null)
    {
        parent::__construct();
        $this->_table   =   $congifs;
        $this->_id      =   "_id";
        $this->_status  =   "_status";
        $this->_update  =   "_update";
        $this->_delete  =   "_delete";
        $this->_insert  =   "_insert";
        if(is_array($congifs)){
            $this->_table   =   isset($congifs["_table"])?$congifs["_table"]:"_unknown";
            $this->_id      =   isset($congifs["_id"])?$congifs["_id"]:"_id";
            $this->_status  =   isset($congifs["_status"])?$congifs["_status"]:"_status";
            $this->_update  =   isset($congifs["_update"])?$congifs["_update"]:"_update";
            $this->_delete  =   isset($congifs["_delete"])?$congifs["_delete"]:"_delete";
            $this->_insert  =   isset($congifs["_insert"])?$congifs["_insert"]:"_insert";
        }
    }
    function tolog($alog){
        $alog["_insert"]=date("Y-m-d H:i:s");
        @$this->db
            ->insert("_log", $alog); 
        $count = @$this->db->affected_rows(); //should return the number of rows affected by the last query
        if($count==1) return true;
        return false;
    }
    function get($_id=""){
        $where=array($this->_id=>$_id);
        $query=$this->db->get_where($this->_table, $where); 
        return $query->result();
    }
    function insert($Params){
        $Params[$this->_insert]=date("Y-m-d H:i:s");
        $this->db
            ->insert($this->_table, $Params); 
        $count = $this->db->affected_rows(); //should return the number of rows affected by the last query
        $alog=array(
            "_table"=>$this->_table,
            "_row"=>null,
            "_commandtype"=>"insert",
            "_log"=>print_r(array(
                "user" =>$_SESSION["ADP"]["USER"]["Name"],
                "ip"=>  getIP(),
                "command"=>"insert"
            ),true)
        );
        if($count==1) {
            @$this->tolog($alog);
            return true;
        }
        return false;
    }
    function update($_id,$Params){
        $Params[$this->_update]=date("Y-m-d H:i:s");
        if(is_array($_id)){
            $this->db->where_in($this->_id, $_id);
        }else{
            $this->db->where($this->_id, $_id);
        }
        $this->db->update($this->_table, $Params); 
        $count = $this->db->affected_rows(); //should return the number of rows affected by the last query
        if($count>0) {
            return $count;
        }
        return false;
    }
    function insert_onduplicate_update($aParamsi,$aUpdate){
        $this->db->on_duplicate_update($this->_table, $aUpdate, $aParamsi); 
        $count = $this->db->affected_rows(); //should return the number of rows affected by the last query
        if($count>0){
            $alog=array(
                "_table"=>$this->_table,
                "_row"=>null,
                "_commandtype"=>"insert_on_update",
                "_log"=>print_r(array(
                    "user" =>$_SESSION["ADP"]["USER"]["Name"],
                    "ip"=>  getIP()
                ),true)
            );
            @$this->tolog($alog);
            return true;
        }
        return false;
    }
    function updateColumn($_id,$col,$val){
        if(is_array($_id)){
            $this->db->where_in($this->_id, $_id);
        }else{
            $this->db->where($this->_id, $_id);
        }
        $Params[$col]=$val;
        $Params[$this->_update]=date("Y-m-d H:i:s");
        $this->db->update($this->_table, $Params); 
        $count = $this->db->affected_rows(); //should return the number of rows affected by the last query
        if($count>0){
//            $alog=array(
//                "_table"=>$this->_table,
//                "_row"=>null,
//                "_commandtype"=>"update",
//                "_log"=>print_r(array(
//                    "user" =>$_SESSION["ADP"]["USER"]["Name"],
//                    "ip"=>  getIP(),
//                    "id"=>  $_id,
//                    "col"=>$col,
//                    "val"=>$val
//                ),true)
//            );
//            @$this->tolog($alog);
            return $count;
        }
        return false;
    }
    function binding_log($_table=null){
        
        $strQuery="
            SELECT SQL_CALC_FOUND_ROWS `_log`.*
            FROM `_log`
            ";
        
        if($_table!==null){
            $_table=  mysql_escape_string($_table);
            $strWhere="
            WHERE  `_table` ='$_table'
            ";
        }else{
            $strWhere="WHERE true";
        }
        $strOrderBy="Order By `_insert` DESC ";
        $strGroupBy="";
        $config=array(
            "strQuery"=>$strQuery,
            "strWhere"=>$strWhere,
            "strOrderBy"=>$strOrderBy,
            "strGroupBy"=>$strGroupBy,
            "usingLimit"=>true,
            "fields"=>array(),
            "datefields"=>array()
        );
        $this->init($config);
        return $this->jqxBinding();
    }
}
?>
